
//
//  AddFoodsTableViewController.swift
//  beegin2
//
//  Created by zsy on 16/3/12.
//  Copyright © 2016年 kszz. All rights reserved.
//

import UIKit

class AddFoodsTableViewController: UITableViewController ,UITextFieldDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func leftNavClick(sender: AnyObject) {
        
        self.navigationController?.popViewControllerAnimated(true)
    }

    // MARK: - Table view data source
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        
        if indexPath.section == 0 && indexPath.row == 0{
            
            //self.performSegueWithIdentifier("showMilkBrandSegue", sender: self)
        }
        
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        
        return 4
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        let rowsArr = [1,4,2,1]
        
        return rowsArr[section]
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        if indexPath.section == 3 {
        
            return 100
        }
        
        return 49
    }
    
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 10
    }
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 1
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let identifierArr = [["scanFoodCell"],["foodsInfoCell","foodsInfoCell","foodAddSelCel","foodAddSelCel"],["foodsInfoCell","foodsInfoCell"],["foodAddPicCell"]]
        
        let cell = tableView.dequeueReusableCellWithIdentifier(identifierArr[indexPath.section][indexPath.row], forIndexPath: indexPath)

        // Configure the cell...
        switch indexPath.section{
        case 0:
            break
            
            
        case 1:
            switch indexPath.row{
            case 0,1:
                if let nameLabel = cell.viewWithTag(11) as? UILabel{
                
                    nameLabel.text = indexPath.row == 0 ? "名称*" : "品牌*"
                }
                if let field = cell.viewWithTag(12) as? UITextField{
                
                    field.placeholder = indexPath.row == 0 ? "无需填品牌" : "中文/英文只需选填一种"
                }
            case 2,3:
                cell.textLabel?.text = indexPath.row == 2 ? "适用阶段*" : "类别*"
                cell.detailTextLabel?.text = indexPath.row == 2 ? "6M+" : "配方奶"
            default: break
            }
            
            
        case 2:
            if let nameLabel = cell.viewWithTag(11) as? UILabel{
                
                nameLabel.text = indexPath.row == 0 ? "版本" : "产地"
            }
            if let field = cell.viewWithTag(12) as? UITextField{
                
                field.placeholder = indexPath.row == 0 ? "例如：2016新版" : "原产国"
            }
            
        case 3:
            if let imageView = cell.viewWithTag(22){
            
                imageView.layer.cornerRadius = imageView.frame.width / 2
            }
            
            
        default: break
            
        }

        return cell
    }
    

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */
    
    // MARK: - Search textfield
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        
        textField.resignFirstResponder()
        return true
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
